package manager

import (
	"fmt"
	"sync"
)

var globalManagers *Managers

// NewManagers 创建并返回所有的管理器
func Init() *Managers {

	// 初始化用户管理器
	userMgr, err := NewUserManager()
	if err != nil {
		panic(fmt.Sprintf("Failed to init user manager: %v", err))
	}

	// 初始化用户管理器
	trackMgr, err := NewTrackManager()
	if err != nil {
		panic(fmt.Sprintf("Failed to init user manager: %v", err))
	}

	// 初始化用户管理器
	liveRoomMgr := NewLiveRoomManager()
	if liveRoomMgr == nil {
		panic(fmt.Sprintf("Failed to init user manager: %v", err))
	}

	globalManagers = &Managers{
		connManager:                 NewConnectionManager(),
		scoreManager:                NewScoreManager(),
		eventManager:                NewEventManager(),
		giftManager:                 NewGiftManager(),
		levelManager:                NewLevelManager(),
		userManager:                 userMgr,
		trackManager:                trackMgr,
		liveRoomManager:             liveRoomMgr,
		giftRecordManager:           NewGiftRecordManager(),                     // 添加GiftRecordManager
		douYinFailGiftRecordManager: NewDouYinFailDouYinFailGiftRecordManager(), // 添加GiftRecordManager
		dyTokenManager:              NewDYToKenManager(),                        // 添加DYToKenManager()
		clientErrorRecordManager:    NewClientErrorRecordManager(),              // 添加ClientErrorRecordManager()
	}

	return globalManagers
}

// 全局配置获取方法
func Get() *Managers {
	if globalManagers == nil {
		panic("managers not initialized")
	}
	return globalManagers
}

// Managers 结构体包含所有管理器
type Managers struct {
	connManager                 *ConnectionManager
	scoreManager                *ScoreManager
	eventManager                *EventManager
	giftManager                 *GiftManager
	levelManager                *LevelManager
	userManager                 *UserManager
	liveRoomManager             *LiveRoomManager
	giftRecordManager           *GiftRecordManager
	douYinFailGiftRecordManager *DouYinFailGiftRecordManager
	trackManager                *TrackManager
	dyTokenManager              *DYToKenManager
	clientErrorRecordManager    *ClientErrorRecordManager
	mu                          sync.Mutex
}

// 提供方法来获取各个管理器
func (m *Managers) ConnectionManager() *ConnectionManager {
	return m.connManager
}

func (m *Managers) ScoreManager() *ScoreManager {
	return m.scoreManager
}

func (m *Managers) EventManager() *EventManager {
	return m.eventManager
}

func (m *Managers) GiftManager() *GiftManager {
	return m.giftManager
}

func (m *Managers) LevelManager() *LevelManager {
	return m.levelManager
}

func (m *Managers) UserManager() *UserManager {
	return m.userManager
}

// LiveRoomManager 获取 LiveRoomManager 实例
func (m *Managers) LiveRoomManager() *LiveRoomManager {
	return m.liveRoomManager
}

// GiftRecordManager 获取 GiftRecordManager 实例
func (m *Managers) GiftRecordManager() *GiftRecordManager {
	return m.giftRecordManager
}

// DouYinFailGiftRecordManager 实例
func (m *Managers) DouYinFailGiftRecordManager() *DouYinFailGiftRecordManager {
	return m.douYinFailGiftRecordManager
}

// DyTokenManager 获取 DYToKenManager 实例
func (m *Managers) DyTokenManager() *DYToKenManager {
	return m.dyTokenManager
}

// ClientErrorRecordManager 实例
func (m *Managers) ClientErrorRecordManager() *ClientErrorRecordManager {
	return m.clientErrorRecordManager
}
